

[CmdletBinding(PositionalBinding=$false)]
Param(
  [Parameter(Mandatory)]
  $TargetGDBServerPort,
  [Parameter(Mandatory)]
  $TargetADBUSBSerialNumber,
  [Parameter(Mandatory)]
  $ExecPathLocal,
  [Parameter(Mandatory)]
  $ExecPathRemote
)

$ADBArgs=""

if($TargetADBUSBSerialNumber.length -gt 0){
  $ADBArgs=$ADBArgs+"-s ${TargetADBUSBSerialNumber} "
}

$ADBArgArr=("${ADBArgs}shell" -split ' ')

$ExecName=Split-Path ${ExecPathRemote} -Leaf


adb ${ADBArgArr} killall ${ExecName}
adb ${ADBArgArr} "ProcPID=`$(ss -tlnp | grep LISTEN | grep ${TargetGDBServerPort} | sed -n 's/.*pid=\([0-9]\+\).*/\1/p') && if [[ \`"`$ProcPID\`" != \`"\`" ]]; then echo kill `$ProcPID;kill `$ProcPID; else echo no process Port: ${TargetGDBServerPort}; fi"

adb ("${ADBArgs}push" -split ' ') ${ExecPathLocal} ${ExecPathRemote}
if($LASTEXITCODE -ne 0){
  exit $LASTEXITCODE
}

adb ${ADBArgArr} chmod +x ${ExecPathRemote}
if($LASTEXITCODE -ne 0){
  exit $LASTEXITCODE
}

